<template>
  <div class="upload-container">
    <div class="page-header">
      <div class="header-content">
        <h2>上传资源</h2>
        <p>上传战术训练所需的资源文件</p>
      </div>
      <div class="header-actions">
        <el-button @click="goBack">返回</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="uploading">
          确认上传
        </el-button>
      </div>
    </div>

    <div class="upload-wrapper">
      <el-card title="文件上传">
        <el-upload
          ref="uploadRef"
          class="upload-demo"
          drag
          :multiple="true"
          :auto-upload="false"
          :on-change="handleFileChange"
          :file-list="fileList"
        >
          <el-icon class="el-icon--upload"><upload-filled /></el-icon>
          <div class="el-upload__text">
            拖拽文件到此处或 <em>点击上传</em>
          </div>
          <template #tip>
            <div class="el-upload__tip">
              支持多种文件类型：脚本、配置、镜像、文档、工具等
            </div>
          </template>
        </el-upload>
      </el-card>

      <el-card title="资源信息" v-if="fileList.length > 0">
        <div v-for="(file, index) in fileList" :key="index" class="file-config">
          <el-form :model="file.config" label-width="100px">
            <div class="file-header">
              <h4>{{ file.name }}</h4>
              <el-button type="danger" size="small" @click="removeFile(index)">移除</el-button>
            </div>
            
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="资源类型">
                  <el-select v-model="file.config.type" placeholder="请选择类型">
                    <el-option label="脚本文件" value="script" />
                    <el-option label="配置文件" value="config" />
                    <el-option label="镜像文件" value="image" />
                    <el-option label="文档资料" value="document" />
                    <el-option label="工具软件" value="tool" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="访问权限">
                  <el-select v-model="file.config.permission" placeholder="请选择权限">
                    <el-option label="公开" value="public" />
                    <el-option label="私有" value="private" />
                    <el-option label="仅团队" value="team" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            
            <el-form-item label="资源描述">
              <el-input
                v-model="file.config.description"
                type="textarea"
                :rows="3"
                placeholder="请输入资源描述"
              />
            </el-form-item>
            
            <el-form-item label="标签">
              <el-input
                v-model="file.config.tags"
                placeholder="多个标签用逗号分隔"
              />
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { UploadFilled } from '@element-plus/icons-vue'

const router = useRouter()

// 响应式数据
const uploadRef = ref()
const uploading = ref(false)
const fileList = ref([])

// 方法
const handleFileChange = (file, files) => {
  // 为新文件添加配置对象
  if (!file.config) {
    file.config = {
      type: '',
      permission: 'private',
      description: '',
      tags: ''
    }
  }
  fileList.value = files
}

const removeFile = (index) => {
  fileList.value.splice(index, 1)
}

const handleSubmit = async () => {
  if (fileList.value.length === 0) {
    ElMessage.warning('请选择要上传的文件')
    return
  }

  // 验证每个文件的配置
  for (const file of fileList.value) {
    if (!file.config.type) {
      ElMessage.warning(`请为文件"${file.name}"选择资源类型`)
      return
    }
    if (!file.config.description) {
      ElMessage.warning(`请为文件"${file.name}"填写描述`)
      return
    }
  }

  uploading.value = true
  
  try {
    // 模拟上传过程
    await new Promise(resolve => setTimeout(resolve, 3000))
    
    ElMessage.success('资源上传成功')
    goBack()
  } catch (error) {
    ElMessage.error('上传失败')
  } finally {
    uploading.value = false
  }
}

const goBack = () => {
  router.push('/tactical-resource/list')
}
</script>

<style lang="scss" scoped>
.upload-container {
  padding: 20px;
  background-color: var(--bg-color);
}

  .header-content {
    h2 {
      margin: 0 0 8px 0;
      color: var(--text-color);
      font-size: 24px;
      font-weight: 600;
    }

    p {
      margin: 0;
      color: var(--text-color-secondary);
      font-size: 14px;
    }
  }


.upload-wrapper {
  .el-card {
    margin-bottom: 20px;
  }
}

.upload-demo {
  :deep(.el-upload-dragger) {
    padding: 40px;
  }
}

.file-config {
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 20px;
  background-color: var(--bg-color-secondary);

  .file-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;

    h4 {
      margin: 0;
      color: var(--text-color);
    }
  }
}
</style> 